package com.kede.pay.controller;

import com.kede.pay.domain.TransferLog;
import com.kede.pay.pojo.po.PayPO;
import com.kede.pay.pojo.po.VipPayPO;
import com.kede.pay.service.IPayNotifyService;
import com.kede.pay.service.WxPayService;
import com.kede.pojo.ResHttp;
import com.kede.utils.ValidList;
import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;


/**
 * 微信支付
 */
@RestController
@RequestMapping("/wxpay")
@Slf4j
public class WxPayController {

    @Resource
    private WxPayService wxPayService;

    @Resource
    private IPayNotifyService payNotifyService;

    /**
     * 发起批量转账
     * @param transferLogList
     * @return
     */
    @PostMapping("/addBatchTransfer")
    public ResHttp BatchTransfer(@RequestBody @Validated ValidList<TransferLog> transferLogList){
        try{
            InitiateBatchTransferResponse initiateBatchTransferResponse = wxPayService.initiateBatchTransfer(transferLogList);
            return ResHttp.okData(initiateBatchTransferResponse);
        }catch (Exception e){
            e.printStackTrace();
            log.error("发起批量转账接口出错==={}",e.getMessage());
            return ResHttp.failMsg("出现异常请联系管理员");
        }

    }

    /**
     * 查询转账订单状态
     * @param userid
     * @return
     */
    @GetMapping("/getTranferStatusByUserId")
    public ResHttp getTranferStatusByUserId(Integer userid){
        wxPayService.getTransferDetailByUserid(userid);
        return ResHttp.ok();
    }

    /**
     * 获取手续费率
     * @return
     */
    @GetMapping("getWidthDrawRate")
    public ResHttp getWidthDrawRate(){
        BigDecimal withdrawalRate = wxPayService.getWithdrawalRate();
        return ResHttp.okData(withdrawalRate);
    }

    /**
     * 会员充值相关
     * attach 必须是包含userid 的对象字符串
     * @param vipPayPO
     * @return
     */
    @PostMapping("vipPayment")
    public ResHttp vipPayment(@RequestBody @Validated VipPayPO vipPayPO){
        return wxPayService.vipPayment(vipPayPO);
    }

    @PostMapping("notify")
    public Map<String,String> Notify(HttpServletRequest request, @RequestBody Map<String, Object> map, HttpServletResponse response){
        return payNotifyService.Vail(request,response, map);
    }

    @PostMapping("Payment")
    public ResHttp Payment(@RequestBody @Validated PayPO payPO){
        return wxPayService.payment(payPO);
    }


}
